<!--!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">-->
<html>
	<head>
		<title>Health Promotion | Home</title>
		<link rel="stylesheet" href="style.css">
		<script src="jquery-1.11.0.js"></script>	
		<script src="gen.js"></script>
		<script>
			//make a synchronus call to the page u and return the
			//result as an object
			var V_id;
			function syncAjax(u)
			{
				var obj=$.ajax(
					{url:u, async:false
					});
                                console.log(u);
				return $.parseJSON(obj.responseText)

			}

            function add(obj)
            {
                //find where the user clicked and store it in x and y
                var y=event.clientY;
                var x=event.clientX/2;
                //use x and y to set the location of the form
                $("#divAdd").css("top",y);
                $("#divAdd").css("left",x);
                //display the form
                $("#divAdd").fadeIn(1000);

                //console.log(v);
                //r=syncAjax(obj);
                //window.location.reload();
                //cancel();
            }

            function addYes()
            {
                var rdate = document.getElementById("hdate").value;
                var venue = document.getElementById("hvenue").value;
                var topic = document.getElementById("htopic").value;
                var method = document.getElementById("hmethod").value;
                var target_audience = document.getElementById("htarget_audience").value;
                var number_of_audience = document.getElementById("hnumber_of_audience").value;
                var remarks = document.getElementById("hremarks").value;
                var month = document.getElementById("hmonth").value;
                var latitude = document.getElementById("hlatitude").value;
                var longitude = document.getElementById("hlongitude").value;
                var image = document.getElementById("himage").value;
                var idcho = document.getElementById("hidcho").value;
                var subdistrict_id = document.getElementById("hsubdistrict_id").value;

                var v="health_promo_action.php?cmd=5&rdate="+rdate
                    +"&venue="+venue
                    +"&topic="+topic
                    +"&method="+method
                    +"&target_audience="+target_audience
                    +"&number_of_audience="+number_of_audience
                    +"&remarks="+remarks
                    +"&month="+month
                    +"&latitude="+latitude
                    +"&longitude="+longitude
                    +"&image="+image
                    +"&idcho="+idcho
                    +"&subdistrict_id="+subdistrict_id;

                r=syncAjax(v);

                window.location.reload();
                console.log(r);
                addNo();
            }


            function addNo()
            {
                $("#divAdd").fadeOut(500);
            }


			function edit(obj,id)
			{
				//get health promotion will make a request to
				//the server
				V_id = id;
				var r=getHealthPromotion(id);
				
				//r is the id from when the edit is clicked
				if(r.result==0)
				{
					//show error message
					$("#divStatus").text(r.message);
                    console.log(r);
					return;
				}
				//if r != 0 then it should get the data from the database
				//and display it in the form
                                $("#date").prop("value", r.health_promotion.pdate);
				$("#venue").prop("value", r.health_promotion.venue);
                                $("#topic").prop("value", r.health_promotion.topic);
				$("#method").prop("value", r.health_promotion.method);
				$("#target_audiece").prop("value", r.health_promotion.target_audience);
				$("#number_of_audience").prop("value", r.health_promotion.number_of_audience);
				$("#remarks").prop("value", r.health_promotion.remarks);
				$("#latitude").prop("value", r.health_promotion.latitude);
				$("#longitude").prop("value", r.health_promotion.longitude);
				$("#image").prop("value", r.health_promotion.image);
				$("#idcho").prop("value", r.health_promotion.idcho);
				$("#subdistrict_id").prop("value", r.health_promotion.subdistrict_id);
				//show the form
				//find where the user clicked and store it in x and y
				var y=event.clientY;
				var x=event.clientX/2;
				//use x and y to set the location of the form
				$("#divEdit").css("top",y);
				$("#divEdit").css("left",x);
				//display the form
				$("#divEdit").fadeIn(1000);
				
			}
			//returns a result object for one health promotion
			function getHealthPromotion(id)
			{
				var u="health_promo_action.php?cmd=1&idhealth_promotion="+id;
                console.log(u);
				return syncAjax(u);
			}
			
			//hides the form
			function cancel()
			{
				//fade out the form in half a second
				$("#divEdit").fadeOut(500);
			}

            function deleteYes()
            {
                var x="health_promo_action.php?cmd=4&idhealth_promotion="+V_id;
                r=syncAjax(x);
                $.getJSON(x, saveDone);
                window.location.reload();
                cancelDelete();
            }

            function deleteNo()
            {
                $("#divDel").fadeOut(500);
            }

            //delete a row from the table
            function delete_health_promo(obj, id, event)
            {
                V_id = id;
                var r=getHealthPromotion(id);

                if(r.result==0)
                {
                    $("#divStatus").text(r.message);
                    return;
                }

                $("idhealth_promotion").prop("value", r.health_promotion.id);
                var y=event.clientY;
                var x=event.clientX/2;

                $("#divDel").css("top", y);
                $("#divDel").css("left", x);
                $("#divDel").fadeIn(1000);

            }


            function cancelDelete()
			{
				$("#divDel").fadeOut(500);
			}
			
			//makes asynchronous calls to the save page
			function save()
			{
                var pdate = document.getElementById("date").value;
				var venue = document.getElementById("venue").value;
				var topic = document.getElementById("topic").value;
                var method = document.getElementById("method").value;
				var target_audience = document.getElementById("target_audience").value;
				var number_of_audience = document.getElementById("number_of_audience").value;
				var remarks = document.getElementById("remarks").value;
				var month = document.getElementById("month").value;
				var latitude = document.getElementById("latitude").value;
				var longitude = document.getElementById("longitude").value;
				var image = document.getElementById("image").value;
				var idcho = document.getElementById("idcho").value;
				var subdistrict_id = document.getElementById("subdistrict_id").value;
				
				var v="health_promo_action.php?cmd=2&idhealth_promotion="+V_id
				+"&date="+pdate
				+"&venue="+venue
				+"&topic="+topic
				+"&method="+method
				+"&target_audience="+target_audience
				+"&number_of_audience="+number_of_audience
				+"&remarks="+remarks
				+"&month="+month
				+"&latitude="+latitude
				+"&longitude="+longitude
				+"&image="+image
				+"&idcho="+idcho
				+"&subdistrict_id="+subdistrict_id;
				
				r=syncAjax(v);
				window.location.reload();
				cancel();
			}
			

			function saveDone(data)
			{
				alert(data);
			}
		
		</script>
	</head>
	<body>
		<table>
			<tr>
				<td colspan="2" id="pageheader">
					Health Promotion Home
				</td>
			</tr>
			<tr>
				<td id="mainnav">
					<a href="index.php"><div class="menuitem">Back to HIS homepage</div></a>
                    <a href="health_promotion_search.php"><div class="menuitem">Search</div></a>
					<a href="health_promo_list.php"><div class="menuitem">All Health Promotions</div></a>
					<div class="menuitem"></div>
					<a href="Promotion_Form.php"><div class="menuitem">Add Promotion</div></a>
					<a href="health_promo_list.php"><div class="menuitem">Edit Promotion</div></a>
					<div class="menuitem">Question and Answers</div>
					<!--<div class="menuitem">family planning</div>-->
				</td>
				<td id="content">
					<div id="divPageMenu">
						<!--<span class="menuitem" >sub districts</span>
						<span class="menuitem" >communities</span>
						<span class="menuitem" >view map</span>-->
						<!--<input type="text" id="txtSearch">-->
						<!--<a href="health_promotion_search.php"><span class="menuitem">Search</span><a/>-->
                        <span class="menuitem" ><span class="hotspot" onclick="add(this)">Add a promotion here</span></span>
					</div>
					<div id="divStatus" class="status">
						Welcome!
					</div>
					<div id="divContent">
						Health Promotions
						<table class="reportTable" width="100%" >
							<tr class="header" >
								<td>ID</td>
								<td>Promotion</td>
								<td></td>
								<td></td>
							</tr>
<?php	
	include("health_promo_class.php");
	$obj=new health_promo_class();
	if(!$obj->get_all_health_promo()){
		echo "error";
		exit();
	}
	
	$row=$obj->fetch();
	$row_counter=0;
	while($row){
		if($row_counter%2==0){
			$style=" class='row1' ";
		}else{
			$style=" class='row2'  ";
		}	
		$id=$row['idhealth_promotion'];
		echo "<tr $style >";
			echo "<td>$id</td>";
			echo "<td><a href='health_promo_list.php?id=$id'>$row[topic]</a></td>";
			echo "<td><span class='hotspot' onclick='edit(this,$id)'>edit</span></td>";
			echo "<td><span class='hotspot' onclick='delete_health_promo(this, $id, event)'>delete</span></td>";
		echo "</tr>";
		$row=$obj->fetch();
         $row_counter++;
	}
?>
                        </table>
                    </div>
                </td>
            </tr>
	    </table>
						<div id="divDel" class="popupForm">
							<table class="tableForm">
								<tr>
									<td class="label"> Are you sure you want to delete?</td></tr>
									<!--<td class="label"></td>-->
									<tr><td class="field">
										<input type="button" value="Yes" onclick="deleteYes()"/>
										<input type="button" value="No" onclick="deleteNo()"/>
									</td>
								</tr>
							</table>
						</div>
						
						<div id="divEdit" class="popupForm">
							<table class="tableForm">
								<tr>
									<td class="label">Date:</td>
									<td class="field">
                                        <input type="date" value="" id="date"/></td>


									<td class="label">Venue:</td>
									<td class="field">
                                        <input type="text" value="" id="venue"/></td>
								</tr>
								<tr>
									<td class="label">Topic:</td>
									<td class="field">
                                        <input type="text" value="" id="topic"/></td>


									<td class="label">Method:</td>
									<td class="field">
                                        <input type="text" value="" id="method"/></td>
								</tr>
								<tr>
									<td class="label">Target Audience:</td>
									<td class="field">
                                        <input type="text" value="" id="target_audience"/></td>


									<td class="label">Number of Audience:</td>
									<td class="field"><input type="text" value="" id="number_of_audience"/></td>
								</tr>
								<tr>
									<td class="label">Remarks:</td>
									<td class="field">
                                        <input type="text" value="" id="remarks"/></td>


									<td class="label">Month:</td>
									<td class="field">
                                        <input type="month" value="" id="month"/></td>
								</tr>
								<tr>
									<td class="label">Latitude:</td>
									<td class="field">
                                        <input type="text" value="" id="latitude"/></td>


									<td class="label">Longitude:</td>
									<td class="field">
                                        <input type="text" value="" id="longitude"/></td>
								</tr>
								<tr>
									<td class="label">Image:</td>
									<td class="field">
                                        <input type="text" value="" id="image"/></td>


									<td class="label">IDcho:</td>
									<td class="field">
                                        <input type="text" value="" id="idcho"/></td>
								</tr>
								<tr>
									<td class="label">Subdistrict ID:</td>
									<td class="field">
                                        <input type="text" value="" id="subdistrict_id"/></td>
								</tr>
								<tr>
									<td class="label"></td>
									<td class="field">
										<input type="button" value="save" onclick="save()"/>
										<input type="button" value="cancel" onclick="cancel()"/>
									</td>
								</tr>
							</table>
						</div>



                        <div id="divAdd" class="popupForm">
                            <table class="tableForm">
                                <tr>
                                    <td class="label">Date:</td>
                                    <td class="field">
                                        <input type="date" value="" id="hdate"/></td>


                                    <td class="label">Venue:</td>
                                    <td class="field">
                                        <input type="text" value="" id="hvenue"/></td>
                                </tr>
                                <tr>
                                    <td class="label">Topic:</td>
                                    <td class="field">
                                        <input type="text" value="" id="htopic"/></td>


                                    <td class="label">Method:</td>
                                    <td class="field">
                                        <input type="text" value="" id="hmethod"/></td>
                                </tr>
                                <tr>
                                    <td class="label">Target Audience:</td>
                                    <td class="field">
                                        <input type="text" value="" id="htarget_audience"/></td>


                                    <td class="label">Number of Audience:</td>
                                    <td class="field"><input type="text" value="" id="hnumber_of_audience"/></td>
                                </tr>
                                <tr>
                                    <td class="label">Remarks:</td>
                                    <td class="field">
                                        <input type="text" value="" id="hremarks"/></td>


                                    <td class="label">Month:</td>
                                    <td class="field">
                                        <input type="month" value="" id="hmonth"/></td>
                                </tr>
                                <tr>
                                    <td class="label">Latitude:</td>
                                    <td class="field">
                                        <input type="text" value="" id="hlatitude"/></td>


                                    <td class="label">Longitude:</td>
                                    <td class="field">
                                        <input type="text" value="" id="hlongitude"/></td>
                                </tr>
                                <tr>
                                    <td class="label">Image:</td>
                                    <td class="field">
                                        <input type="text" value="" id="himage"/></td>


                                    <td class="label">IDcho:</td>
                                    <td class="field">
                                        <input type="text" value="" id="hidcho"/></td>
                                </tr>
                                <tr>
                                    <td class="label">Subdistrict ID:</td>
                                    <td class="field">
                                        <input type="text" value="" id="hsubdistrict_id"/></td>
                                </tr>
                                <tr>
                                    <td class="label"></td>
                                    <td class="field">
                                        <input type="button" value="Add" onclick="addYes()"/>
                                        <input type="button" value="cancel" onclick="addNo()"/>
                                    </td>
                                </tr>
                            </table>
                        </div>
<?php
    $vid = 0;
        if(isset($_REQUEST['id']))
    {
        $vid = $_REQUEST['id'];
    }
    $obj = new health_promo_class();
    if(!$obj->delete_health_promo($vid))
    {
        echo "error";
        echo mysql_error();
        exit();
    }

?>

        <?php


        if(isset($_REQUEST['date'], $_REQUEST['venue'], $_REQUEST['topic'], $_REQUEST['method'], $_REQUEST['target_audience'],
        $_REQUEST['number_of_audience'], $_REQUEST['remarks'], $_REQUEST['month'], $_REQUEST['latitude'], $_REQUEST['longitude'],
        $_REQUEST['image'], $_REQUEST['id_cho'], $_REQUEST['subdistrict_id'])) {

            $date = $_REQUEST['date'];
            $venue = $_REQUEST['venue'];
            $topic = $_REQUEST['topic'];
            $method = $_REQUEST['method'];
            $target_audience = $_REQUEST['target_audience'];
            $number_of_audience = $_REQUEST['number_of_audience'];
            $remarks = $_REQUEST['remarks'];
            $month = $_REQUEST['month'];
            $latitude = $_REQUEST['latitude'];
            $longitude = $_REQUEST['longitude'];
            $image = $_REQUEST['image'];
            $id_cho = $_REQUEST['id_cho'];
            $subdistrict_id = $_REQUEST['subdistrict_id'];

            $object = new health_promo_class();
            if (!$object->add_health_promo($date, $venue, $topic, $method, $target_audience, $number_of_audience, $remarks, $month,
                $latitude, $longitude, $image, $id_cho, $subdistrict_id)
            ) {
                echo "error";
                echo mysql_error();
                exit();
            }
        }
?>
                                             
	</body>
</html>	